package com.xhh.nphm.common.utils;

import com.xhh.nphm.common.constant.Constants;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @description: 获取电话信件工单编号、流水号
 * @author: sun
 * @date: 2023-05-10 11:12
 * @version 1.0
 */
@Component
public class AcquireWFNoUtils {
    @Value("${workOrder.acquireUrl}")
    public String acquireUrl;

    @Value("${workOrder.enable}")
    public boolean enable;//是否开启

    /**
     * 是否允许获取
     * @return
     */
    public boolean isEnable(){
        return enable;
    }

    /**
     * 获取信件工单流水号
     * @return
     */
    public String getMailWsCode() {
        HttpGet httpGet = new HttpGet(acquireUrl + "mailCode");
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return "";
            }
            entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 获取电话工单流水号
     * @return
     */
    public String getTelWsCode() {
        HttpGet httpGet = new HttpGet(acquireUrl + "telCode");
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return "";
            }
            entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 获取信件工单编号
     * @return
     */
    public String getMailWFNo() {
        HttpGet httpGet = new HttpGet(acquireUrl + "mailNo");
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return "";
            }
            entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 获取电话工单编号
     * @return
     */
    public String getTelWFNo() {
        HttpGet httpGet = new HttpGet(acquireUrl + "telNo");
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return "";
            }
            entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 获取电话工单办理量
     * @return
     */
    public int getTelTotal() {
        HttpGet httpGet = new HttpGet(acquireUrl + "total?key=" + Constants.TEL_TOTAL_KEY);
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return -1;
            }
            entity = response.getEntity();
            return Integer.parseInt(EntityUtils.toString(entity));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 获取信件工单办理量
     * @return
     */
    public int getMailTotal() {
        HttpGet httpGet = new HttpGet(acquireUrl + "total?key=" + Constants.MAIL_TOTAL_KEY);
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return -1;
            }
            entity = response.getEntity();
            return Integer.parseInt(EntityUtils.toString(entity));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 更新初始值
     * @param key
     * @return
     */
    public boolean renewValue(String key) {
        boolean flag = false;
        HttpGet httpGet = new HttpGet(acquireUrl + "renew?key=" + key);
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return false;
            }
            entity = response.getEntity();
            flag = Boolean.parseBoolean(EntityUtils.toString(entity));
            return flag;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }
}
